package com.ludomancy.archeologyrpg;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.Iterator;
import com.ludomancy.gamelib.ResourceManager;
import com.ludomancy.gamelib.Widget;

public class LandscapeWidget extends Widget
{

	public Entity focusedEntity = null;
	private long exploringTime;
	private Image background;
	
	public LandscapeWidget(Widget parent, Rectangle area)
	{
		super(parent, area);
		background = ResourceManager.getImage("background.png");
	}
	
	public void mouseMoved(Point pos)
	{
		Entity e = getEntityUnder(pos);
		focusedEntity = e;
		if(focusedEntity != null)
		{
			ArcheologyRPGApplet.mainWidget.info.setText(focusedEntity.getInfo());
		}
		else
		{
			ArcheologyRPGApplet.mainWidget.info.setText(null);
		}
	}

	public void mouseUp(Point pos)
	{
		Game.game.activeEntity = null;
		Game.game.exploringPoint = null;
		mouseRelease();
	}

	public void mouseDown(Point pos)
	{
		Entity e = getEntityUnder(pos);
		Game.game.activeEntity = e;
		if(isWithin(pos))
		{
			if(e == null)
			{
				// exploration
				Game.game.exploringPoint = pos;
				exploringTime = System.currentTimeMillis();
			}
			mouseCapture();
		}
	}
	
	private boolean isWithin(Point pos)
	{
		return pos.x >= 0 && pos.x < Game.LANDSCAPE_WIDTH * Game.TILE_SIZE && pos.y >= 0 && pos.y < Game.LANDSCAPE_HEIGHT;
	}
	
	public Entity getEntityUnder(Point cursor)
	{
		for (Iterator iter = Game.game.entities.iterator(); iter.hasNext();)
		{
			Entity entity = (Entity) iter.next();
			
			Rectangle area = new Rectangle(entity.px - Game.ENTITY_TILE_SIZE/2, entity.py - Game.ENTITY_TILE_SIZE/2, Game.ENTITY_TILE_SIZE, Game.ENTITY_TILE_SIZE);
			if(area.inside(cursor.x, cursor.y) && entity.isVisible())
			{
				return entity;
			}
		}
		return null;
	}

	public void paint(Graphics g)
	{
		// background
		g.drawImage(background, 0, 0, null);
		// paint entities
		for (Iterator iter = Game.game.entities.iterator(); iter.hasNext();)
		{
			Entity entity = (Entity) iter.next();
			if(entity.isVisible())
			{
				entity.paint(g);
			}
		}
		for (Iterator iter = Game.game.entities.iterator(); iter.hasNext();)
		{
			Entity entity = (Entity) iter.next();
			if(entity.isVisible())
			{
				entity.paintInfo(g);				
			}
		}
		// paint focused entity verb(if any)
		if(focusedEntity != null && focusedEntity.isVisible())
		{
			focusedEntity.paintVerb(g);
		}
		
		// paint exploration radius, if exploring
		if(Game.game.exploringPoint != null)
		{
			g.setColor(Color.GREEN);
			g.drawArc(Game.game.exploringPoint.x - Game.game.maxExplorationRadius, Game.game.exploringPoint.y - Game.game.maxExplorationRadius, Game.game.maxExplorationRadius*2, Game.game.maxExplorationRadius*2, 0, 360);
			g.setColor(Color.WHITE);
			int delta = (int)(System.currentTimeMillis() - exploringTime) % 1200;
			int width = (delta * Game.game.maxExplorationRadius*2) / 1200;   
			g.drawArc(Game.game.exploringPoint.x - width/2, Game.game.exploringPoint.y - width/2, width, width, 0, 360);
		}
	}

}
